﻿using ProTrack.Dto;
using ProTrack.Entity;
using ProTrack.IRepository;
using ProTrack.IRepository.WareHouse;
using ProTrack.IServices.Examine;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ProTrack.Services.Examine
{
    /// <summary>
    /// 来料检验单实现接口
    /// </summary>
    public class QcReceiveRecordServices : IQcReceiveRecordServices
    {

        public QcReceiveRecordServices(IBaseRepository db, IQcReceiveRecordRepository qr)
        {

            Db = db;
            Qr = qr;
        }

        public IBaseRepository Db { get; }


        public IQcReceiveRecordRepository Qr { get; }

        //新增
        public async Task<bool> AddAsync(QcReceiveRecord model, List<QcReceiveRecordItemDto> list)
        {
            var result = await Qr.AddQcReceiveRecord(model, list);
            return result;
        }
        //批量删除
        public async Task<bool> DeleteAsync(int[] Ids)
        {
            string sql = "delete QcReceiveRecord where Id in @Ids;delete QcReceiveRecordItemDetails where QcReceiveRecordItemId in (select Id from QcReceiveRecordItem where RecordId in @Ids);delete QcReceiveRecordItem where RecordId in @Ids";
            var result = await Db.DeleteAsync(sql, new { Ids });
            return result;
        }
        //删除
        public async Task<bool> DeleteAsync(int Id)
        {
            string sql = "delete QcReceiveRecord where Id=@Id;delete QcReceiveRecordItemDetails where QcReceiveRecordItemId in (select Id from QcReceiveRecordItem where RecordId=@Id);delete QcReceiveRecordItem where RecordId=@Id";
            var result = await Db.DeleteAsync(sql, new { Id });
            return result;
        }
        //查询所有
        public async Task<IEnumerable<QcReceiveRecord>> GetAllAsync()
        {
            string sql = "select * from QcReceiveRecord";
            var list = await Db.QueryAllAsync<QcReceiveRecord>(sql);
            return list;
        }
        //分页查询
        public async Task<IEnumerable<QcReceiveRecord>> GetAsync(int PageIndex, int PageSize, string RecordCode, string ItemName, string Status)
        {
            string sql = "select * from QcReceiveRecord where 1=1 ";

            if (!string.IsNullOrEmpty(RecordCode))
            {
                sql += " and RecordCode like @RecordCode ";
            }

            if (!string.IsNullOrEmpty(ItemName))
            {
                sql += " and ItemName like @ItemName ";
            }

            if (!string.IsNullOrEmpty(Status))
            {
                sql += " and Status = @Status ";
            }



            sql += " order by CreateTime desc offset @PageSize*(@PageIndex-1) row fetch next @PageSize rows only ";

            var list = await Db.QueryAsync<QcReceiveRecord>(sql, new { PageIndex, PageSize, RecordCode = $"%{RecordCode}%", ItemName = $"%{ItemName}%", Status = $"{Status}" });
            return list;
        }
        //获取总条数
        public async Task<int> GetCountAsync(string RecordCode, string ItemName, string Status)
        {
            string sql = "select count(*) from QcReceiveRecord where 1=1 ";
            if (!string.IsNullOrEmpty(RecordCode))
            {
                sql += " and RecordCode like @RecordCode ";
            }

            if (!string.IsNullOrEmpty(ItemName))
            {
                sql += " and ItemName like @ItemName ";
            }

            if (!string.IsNullOrEmpty(Status))
            {
                sql += " and Status = @Status ";
            }



            var count = await Db.GetRecoreCountAsync(sql, new { RecordCode = $"%{RecordCode}%", ItemName = $"%{ItemName}%", Status = $"{Status}" });
            return count;
        }


        //获取到货通知单下的商品
        public async Task<IEnumerable<WareHouseInboundBillItemDto>> GetItemByWareHouseInboundBillId(string WareHouseInboundBillId)
        {
            var list = await Db.QueryAsync<WareHouseInboundBillItemDto>(
                 @"select w.Id,i.Id as ItemId,i.ItemCode,i.ItemName,i.Model,i.Spec,i.UnitOfMeasure,(w.Quantity-isnull((select SUM(CheckNumber) from QcReceiveRecord where WareHouseInboundBillId=@WareHouseInboundBillId and ItemId=i.Id),0)) as Quantity  from WareHouseInboundBillItem w 
                    inner join Item i on w.ItemId=i.Id
                    where WareHouseInboundBillId=@WareHouseInboundBillId and i.Id not in(
						select ItemId from QcReceiveRecord where Count=(select SUM(CheckNumber) from QcReceiveRecord where WareHouseInboundBillId=@WareHouseInboundBillId) and WareHouseInboundBillId=@WareHouseInboundBillId
                    )", new { WareHouseInboundBillId });
            return list;
        }

        //根据商品Id获取来料检验标准
        public async Task<IEnumerable<QcReceiveStandardItem>> GetQcProcessStandardItemByItemId(string ItemId)
        {
            var list = await Db.QueryAsync<QcReceiveStandardItem>("select  qi.* from QcReceiveStandard q inner join QcReceiveStandardItem qi on q.Id=qi.QcReceiveStandardId where q.ItemId = @ItemId", new { ItemId });
            return list;
        }
        //根据来料检验单Id获取来料检验单子项和缺陷记录明细
        public async Task<IEnumerable<QcReceiveRecordItemDto>> GetQcReceiveRecordItemDetailsByQcReceiveRecordId(string QcReceiveRecordId)
        {

            List<QcReceiveRecordItemDto> list = new List<QcReceiveRecordItemDto>();
            try
            {
                var list2 = await Db.QueryAsync<QcReceiveRecordItem>("select * from QcReceiveRecordItem where  RecordId=@QcReceiveRecordId", new { QcReceiveRecordId });
                foreach (var item in list2)
                {
                    var list3 = await Db.QueryAsync<QcReceiveRecordItemDetailsDto>("select * from QcReceiveRecordItemDetails where QcReceiveRecordItemId=@Id", new { item.Id });

                    QcReceiveRecordItemDto dto = new QcReceiveRecordItemDto()
                    {
                        Device = item.Device,
                        Children = (List<QcReceiveRecordItemDetailsDto>)list3,
                        Classify = item.Classify,
                        Id = item.Id,
                        ProjectCode = item.ProjectCode,
                        ProjectName = item.ProjectName,
                        RecordId = item.RecordId,
                        Remark = item.Remark,
                        StandUnit = item.StandUnit,
                        StandValue = item.StandValue,
                        Status = item.Status
                    };
                    list.Add(dto);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }

            return list;

        }

        //修改
        public async Task<bool> UpdateAsync(QcReceiveRecord model, List<QcReceiveRecordItemDto> list)
        {
            var result = await Qr.UpdateQcReceiveRecord(model, list);
            return result;
        }

        //修改单据状态
        public async Task<bool> UpdateStatus(long Id, int Status)
        {
            string sql = $"update QcReceiveRecord set Status=@Status where Id=@Id";
            var result = await Db.UpdateAsync(sql, new { Id, Status });
            return result;
        }

    }
}
